package cn.com.cifi.mars.web.action.sys;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.com.cifi.mars.bean.bo.SysLogBO;
import cn.com.cifi.mars.bean.vo.sys.req.SaveSysRoleReqParam;
import cn.com.cifi.mars.bean.vo.sys.req.UpdateSysRoleReqParam;
import cn.com.cifi.mars.bean.vo.sys.rsp.SysRoleVo;
import cn.com.cifi.mars.common.base.BaseAction;
import cn.com.cifi.mars.common.bean.R;
import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.CodeEnum;
import cn.com.cifi.mars.constant.LogEventEnum;
import cn.com.cifi.mars.constant.LogSrcEnum;
import cn.com.cifi.mars.entity.common.IdmUser;
import cn.com.cifi.mars.service.IdmPositionGroupService;
import cn.com.cifi.mars.service.SysLogService;
import cn.com.cifi.mars.service.SysRoleService;
import cn.com.cifi.mars.util.WebUtils;
@RestController
public class SysRoleController extends BaseAction implements ISysRoleController{
	private static final Logger logger = LoggerFactory.getLogger(SysRoleController.class);

	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysLogService sysLogService;
	
	/**
	 * 添加角色
	 */
	@Override
	public R<String> createRole(String apiVersion,  SaveSysRoleReqParam param) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		IdmUser user = getOperator();
		// 新增日志
		SysLogBO bo = new SysLogBO();
		bo.setEvent(LogEventEnum.ADD);
		bo.setSource(LogSrcEnum.PC);
		bo.setIp(WebUtils.getIpAddr(request));
		bo.setUrl(WebUtils.getUrl(request));
		bo.setModule("角色管理");
		bo.setTitle("新建角色");
		bo.setContent("新建角色");
		boolean msg = sysLogService.add(bo, user.getAlias());

		return ok(sysRoleService.add(param,user.getAlias()));
	}

	/**
	 * 编辑角色
	 */
	@Override
	public R<String> updateRole(String apiVersion,  UpdateSysRoleReqParam param) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		IdmUser user = getOperator();
		// 新增日志
		SysLogBO bo = new SysLogBO();
		bo.setEvent(LogEventEnum.EDIT);
		bo.setSource(LogSrcEnum.PC);
		bo.setIp(WebUtils.getIpAddr(request));
		bo.setUrl(WebUtils.getUrl(request));
		bo.setModule("角色管理");
		bo.setTitle("编辑角色");
		bo.setContent("编辑角色");
		boolean msg = sysLogService.add(bo,user.getAlias());

		return ok(sysRoleService.update(param, user.getAlias()));
	}

	/**
	 * 查询特殊角色列表分页
	 */
	@Override
	public R<SysRoleVo> getRolePageList(String apiVersion,String current,String pageSize) {
        return ok(sysRoleService.queryRolePage(current,pageSize));
	}

	/**
	 * 删除角色
	 */
	@Override
	public R<String> deleteRoleById(String apiVersion, String roleId) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		IdmUser user = getOperator();
		// 新增日志
		SysLogBO bo = new SysLogBO();
		bo.setEvent(LogEventEnum.DELETE);
		bo.setSource(LogSrcEnum.PC);
		bo.setIp(WebUtils.getIpAddr(request));
		bo.setUrl(WebUtils.getUrl(request));
		bo.setModule("角色管理");
		bo.setTitle("删除角色");
		bo.setContent("删除角色");
		boolean msg = sysLogService.add(bo, user.getAlias());

		return ok(sysRoleService.deleteByRoleId(roleId));
	}

}
